﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;


class FallingRocks
{
    static Random randSymbol = new Random();
    static int[,] rocks = new int[15, 21];
    static char[,] symbols = new char[15, 21];
    static bool gameOver = false;
    static Random chance = new Random();
    static int meY = 9;
    static uint lives = 5;
    static uint score = 0;
    static int level = 1;
    static int levelChance = 110 - level * 10;

    static void userInput()
    {
        if (Console.KeyAvailable)
        {
            ConsoleKeyInfo userMove = Console.ReadKey();
            if (userMove.Key == ConsoleKey.LeftArrow && meY > 0)
            {
                meY--;
            }
            if (userMove.Key == ConsoleKey.RightArrow && meY < 17)
            {
                meY++;
            }
        }
    }

    static void rocksMove()
    {
        for (int i = 0; i < 21; i++)
        {
            if (rocks[13, i] == 1 && i >= meY && i <= meY + 2 && symbols[13, i] != '+')
            {
                if (lives > 0)
                {
                    lives--;
                    for (int y = 0; y < 15; y++)
                    {
                        for (int z = 0; z < 21; z++)
                        {
                            rocks[y, z] = 0;
                        }
                    }
                    break;
                }
                else
                {
                    gameOver = true;
                }
            }
            else if (rocks[13, i] == 1 && i >= meY && i <= meY + 2 && symbols[13, i] == '+')
            {
                lives++;
                score += 10;
                if (score >= 1000)
                {
                    Console.Clear();
                    winGame();
                }
                if (score >= level * 100 && level < 10)
                {
                    level++;
                    levelChance = 110 - level * 10;
                }
            }
            else if (rocks[13, i] == 1 && symbols[13, i] != '+')
            {
                score++;
                if (score >= 1000)
                {
                    Console.Clear();
                    winGame();
                }
                if (score >= level * 100 && level < 10)
                {
                    level++;
                    levelChance = 110 - level * 10;
                }
            }

        }
        for (int i = 13; i >= 0; i--)
        {
            for (int y = 0; y < 21; y++)
            {
                rocks[i + 1, y] = rocks[i, y];
                symbols[i + 1, y] = symbols[i, y];
            }
        }

        for (int i = 0; i < 21; i++)
        {
            rocks[0, i] = chance.Next(0, levelChance);
            if (rocks[0, i] == 1)
            {
                symbols[0, i] = generateSymbols();
            }

        }
    }

    static void printScrean()
    {
        Console.Clear();
        for (int i = 0; i < 15; i++)
        {
            for (int y = 0; y < 21; y++)
            {
                if (rocks[i, y] == 1)
                {
                    Console.SetCursorPosition(y, i);
                    Console.Write(symbols[i, y]);
                }
            }
            Console.WriteLine();
        }

        Console.SetCursorPosition(meY, 14);
        Console.Write("(0)");

        Console.SetCursorPosition(25, 5);
        Console.Write("Level: " + level + @" \ 10");
        Console.SetCursorPosition(25, 7);
        Console.Write("Score: " + score + @" \ 1000");
        Console.SetCursorPosition(25, 9);
        Console.Write("Lives: " + lives);
    }

    static char generateSymbols()
    {
        char charToReturn;
        switch (randSymbol.Next(1, 13))
        {
            case 1: charToReturn = '!'; break;
            case 2: charToReturn = '@'; break;
            case 3: charToReturn = '#'; break;
            case 4: charToReturn = '$'; break;
            case 5: charToReturn = '%'; break;
            case 6: charToReturn = '^'; break;
            case 7: charToReturn = '&'; break;
            case 8: charToReturn = '*'; break;
            case 9: charToReturn = ';'; break;
            case 10: charToReturn = '.'; break;
            case 11: charToReturn = '-'; break;
            default: charToReturn = '+'; break;
        }
        return charToReturn;
    }

    static void GameOver()
    {
        Console.Clear();
        Console.WriteLine("Game Over!!!\nScore: " + score);
    }

    static void winGame()
    {
        while (true)
        {
            Console.WriteLine("YOU WON THE GAME!!!");
            Thread.Sleep(1000);
            Console.Clear();
            Thread.Sleep(250);
        }
    }

    static void Main(string[] args)
    {
        Console.BufferHeight = Console.WindowHeight;
        Console.BufferWidth = Console.WindowWidth;

        for (int i = 0; i < 15; i++)
        {
            for (int y = 0; y < 21; y++)
            {
                rocks[i, y] = 0;
            }
        }
        while (!gameOver)
        {
            rocksMove();
            userInput();
            printScrean();
            Thread.Sleep(150);
        }

        GameOver();
    }
}
